import time
import random
import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException

driver = uc.Chrome()

login_url = 'https://login.taobao.com/member/login.jhtml'
driver.get(login_url)

time.sleep(3)


username = ''
password = ''

username_field = driver.find_element(By.ID, 'fm-login-id')
password_field = driver.find_element(By.ID, 'fm-login-password')

username_field.send_keys(username)
password_field.send_keys(password)

try:
    login_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//button[contains(@class, "fm-button") and contains(text(), "登录")]'))
    )
    login_button.click()
    print("Login button clicked successfully.")
except TimeoutException:
    print("Login button not found within the timeout period.")
    driver.quit()
    exit()

try:
    slider = WebDriverWait(driver, 15).until(
        EC.visibility_of_element_located((By.CLASS_NAME, 'nc_iconfont.btn_slide'))
    )
    print("CAPTCHA slider found. Attempting to slide...")

    time.sleep(2)

    slider_track = driver.find_element(By.CLASS_NAME, 'nc_bg')
    slider_width = slider_track.size['width']
    print(f"Slider track width: {slider_width} pixels")

    action = ActionChains(driver)
    action.click_and_hold(slider).perform()

    total_move = 0
    while total_move < slider_width - 10:  
        move_by = random.randint(5, 10)  
        action.move_by_offset(move_by, 0).perform()
        total_move += move_by
        print(f"Moved slider by {move_by} pixels. Total moved: {total_move} pixels")
        time.sleep(random.uniform(0.1, 0.3))  

    remaining_move = slider_width - total_move
    print(f"Final move to complete: {remaining_move} pixels")
    action.move_by_offset(remaining_move, 0).perform()
    action.r